<template>
  <div class="detail-container">
    <!-- 顶部导航栏 -->
    <header class="header">
      <nav class="nav-container">
        <div class="nav-left">
          <button class="back-btn" @click="goBack">
            <span class="back-icon">←</span>
            返回
          </button>
          <h1 class="page-title">新闻详情</h1>
        </div>
      </nav>
    </header>

    <!-- 主要内容区域 -->
    <main class="main-content">
      <div class="detail-wrapper" v-if="!loading && newsDetail">
        <!-- 标题区域 -->
        <div class="detail-header">
          <h1 class="detail-title">{{ newsDetail.title }}</h1>
          <div class="detail-meta">
            <span class="meta-item">
              <span class="meta-label">发布时间：</span>
              <span class="meta-value">{{ formatDate(newsDetail.publishTime || newsDetail.createTime) }}</span>
            </span>
            <span class="meta-item" v-if="newsDetail.viewCount">
              <span class="meta-label">浏览次数：</span>
              <span class="meta-value">{{ newsDetail.viewCount }}</span>
            </span>
            <span class="meta-item">
              <span class="meta-label">状态：</span>
              <span class="meta-value status" :class="getStatusClass(newsDetail.status)">
                {{ getStatusText(newsDetail.status) }}
              </span>
            </span>
            <span class="meta-item" v-if="newsDetail.author">
              <span class="meta-label">作者：</span>
              <span class="meta-value">{{ newsDetail.author }}</span>
            </span>
          </div>
        </div>

        <!-- 摘要区域 -->
        <div class="detail-summary" v-if="newsDetail.summary">
          <h3>摘要</h3>
          <p>{{ newsDetail.summary }}</p>
        </div>

        <!-- 内容区域 -->
        <div class="detail-content">
          <h3>详细内容</h3>
          <div class="rich-content" v-html="newsDetail.content || '暂无详细内容'"></div>
        </div>
      </div>
      
      <!-- 加载状态 -->
      <div v-if="loading" class="loading-state">
        <div class="loading-spinner"></div>
        <p>加载中...</p>
      </div>

      <!-- 错误状态 -->
      <div v-if="!loading && !newsDetail" class="error-state">
        <div class="error-icon">❌</div>
        <p class="error-text">新闻信息不存在或已被删除</p>
        <button class="error-btn" @click="goBack">返回列表</button>
      </div>
    </main>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useTokenStore } from '@/stores/token.js'
import { getNewsDetailService } from '@/api/home.js'

const router = useRouter()
const route = useRoute()
const tokenStore = useTokenStore()
const newsDetail = ref(null)
const loading = ref(true)

// 返回上一页
const goBack = () => {
  router.go(-1)
}

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return '时间未知'
  const date = new Date(dateString)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

// 获取状态样式类
const getStatusClass = (status) => {
  switch (status) {
    case 1:
      return 'status-published'
    case 0:
      return 'status-draft'
    case 2:
      return 'status-offline'
    default:
      return 'status-default'
  }
}

// 获取状态文本
const getStatusText = (status) => {
  switch (status) {
    case 1:
      return '已发布'
    case 0:
      return '草稿'
    case 2:
      return '已下线'
    default:
      return '未知状态'
  }
}

// 加载新闻详情
const loadNewsDetail = async () => {
  try {
    loading.value = true
    const newsId = route.params.id
    console.log('加载新闻详情，ID:', newsId)
    
    if (!newsId) {
      newsDetail.value = null
      return
    }

    const result = await getNewsDetailService(newsId)
    console.log('新闻详情API响应:', result)
    
    if (result.code === 200 && result.data) {
      newsDetail.value = result.data
      console.log('新闻详情加载成功:', newsDetail.value)
    } else {
      console.log('新闻详情加载失败:', result.message || '未知错误')
      newsDetail.value = null
    }
  } catch (error) {
    console.error('加载新闻详情失败:', error)
    newsDetail.value = null
  } finally {
    loading.value = false
  }
}

onMounted(async () => {
  // 检查登录状态
  if (!tokenStore.token) {
    router.push('/login')
    return
  }
  
  await loadNewsDetail()
})
</script>

<style scoped>
.detail-container {
  min-height: 100vh;
  background: #f5f5f5;
}

/* 顶部导航栏 */
.header {
  background: linear-gradient(135deg, #6278db 0%, #635da6 100%);
  padding: 0;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.nav-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 15px 20px;
}

.nav-left {
  display: flex;
  align-items: center;
  gap: 20px;
}

.back-btn {
  background: rgba(255, 255, 255, 0.2);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.3);
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;
}

.back-btn:hover {
  background: rgba(255, 255, 255, 0.3);
}

.back-icon {
  font-size: 16px;
}

.page-title {
  color: white;
  font-size: 20px;
  font-weight: 600;
  margin: 0;
}

/* 主要内容区域 */
.main-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 30px 20px;
}

.detail-wrapper {
  background: white;
  border-radius: 15px;
  padding: 40px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

/* 标题区域 */
.detail-header {
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 2px solid #f0f0f0;
}

.detail-title {
  color: #333;
  font-size: 28px;
  font-weight: 700;
  margin: 0 0 15px 0;
  line-height: 1.4;
}

.detail-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}

.meta-item {
  display: flex;
  align-items: center;
  font-size: 14px;
}

.meta-label {
  color: #666;
  margin-right: 5px;
}

.meta-value {
  color: #333;
  font-weight: 500;
}

.meta-value.status {
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
}

.status-published {
  background: #d4edda;
  color: #155724;
}

.status-draft {
  background: #fff3cd;
  color: #856404;
}

.status-offline {
  background: #f8d7da;
  color: #721c24;
}

.status-default {
  background: #e2e3e5;
  color: #495057;
}

/* 摘要区域 */
.detail-summary {
  margin-bottom: 30px;
  padding: 20px;
  background: #f8f9fa;
  border-left: 4px solid #28a745;
  border-radius: 0 8px 8px 0;
}

.detail-summary h3 {
  color: #333;
  font-size: 18px;
  margin: 0 0 10px 0;
  font-weight: 600;
}

.detail-summary p {
  color: #666;
  font-size: 16px;
  line-height: 1.6;
  margin: 0;
}

/* 内容区域 */
.detail-content {
  margin-bottom: 20px;
}

.detail-content h3 {
  color: #333;
  font-size: 20px;
  margin: 0 0 20px 0;
  font-weight: 600;
}

/* 富文本内容样式 */
.rich-content {
  color: #333;
  font-size: 16px;
  line-height: 1.8;
}

.rich-content :deep(h1),
.rich-content :deep(h2),
.rich-content :deep(h3),
.rich-content :deep(h4),
.rich-content :deep(h5),
.rich-content :deep(h6) {
  color: #333;
  margin: 20px 0 15px 0;
  font-weight: 600;
}

.rich-content :deep(p) {
  margin: 15px 0;
  line-height: 1.8;
}

.rich-content :deep(img) {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  margin: 15px 0;
}

.rich-content :deep(ul),
.rich-content :deep(ol) {
  margin: 15px 0;
  padding-left: 30px;
}

.rich-content :deep(li) {
  margin: 8px 0;
  line-height: 1.6;
}

.rich-content :deep(blockquote) {
  margin: 20px 0;
  padding: 15px 20px;
  background: #f8f9fa;
  border-left: 4px solid #28a745;
  border-radius: 0 8px 8px 0;
  color: #666;
}

.rich-content :deep(table) {
  width: 100%;
  border-collapse: collapse;
  margin: 20px 0;
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.rich-content :deep(th),
.rich-content :deep(td) {
  padding: 12px 15px;
  text-align: left;
  border-bottom: 1px solid #e9ecef;
}

.rich-content :deep(th) {
  background: #f8f9fa;
  font-weight: 600;
  color: #333;
}

/* 加载状态 */
.loading-state {
  text-align: center;
  padding: 60px 20px;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #28a745;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 20px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 错误状态 */
.error-state {
  text-align: center;
  padding: 60px 20px;
}

.error-icon {
  font-size: 64px;
  margin-bottom: 20px;
  opacity: 0.6;
}

.error-text {
  color: #999;
  font-size: 16px;
  margin: 0 0 20px 0;
}

.error-btn {
  background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 20px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.error-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(40, 167, 69, 0.3);
}

/* 移动端适配 */
@media (max-width: 768px) {
  .nav-container {
    padding: 12px 15px;
  }
  
  .page-title {
    font-size: 18px;
  }
  
  .main-content {
    padding: 20px 15px;
  }
  
  .detail-wrapper {
    padding: 25px 20px;
  }
  
  .detail-title {
    font-size: 24px;
  }
  
  .detail-meta {
    flex-direction: column;
    gap: 10px;
  }
  
  .meta-item {
    font-size: 13px;
  }
}

@media (max-width: 480px) {
  .nav-left {
    gap: 15px;
  }
  
  .back-btn {
    padding: 6px 12px;
    font-size: 13px;
  }
  
  .page-title {
    font-size: 16px;
  }
  
  .detail-wrapper {
    padding: 20px 15px;
  }
  
  .detail-title {
    font-size: 20px;
  }
  
  .detail-summary {
    padding: 15px;
  }
  
  .rich-content {
    font-size: 15px;
  }
}
</style>
